/**
 * 
 */
var signinApp = angular.module('SigninApp', ['ui.bootstrap']);

signinApp.controller('SigninCtrl',
	    ['$scope', '$http', '$window',
	    function($scope, $http, $window)
	    {
			$scope.userName = "";
			$scope.password = "";
			
			$scope.init = function(){
				// TODO: initialization stuff
			}
			
			$scope.signin = function() {
				if(!($scope.signinForm.txtUserName.$valid)) {
					Utility.showAlert("alertContainer", "danger", true, 0, "<strong>User Name</strong> is a required field!");
					return;
				}
				else if(!($scope.signinForm.txtPassword.$valid)) {
					Utility.showAlert("alertContainer", "danger", true, 0, "<strong>Password</strong> is a required field!");
					return;
				}
				else {
					$http({
		                method: "POST",
		                url: "/PatientFirst/services/userservice/login",
		                data: {"userName":$scope.userName, "password":$scope.password}
		            }).success(
		                function(responseModel, status, headers, config) {
		                    if(responseModel.status == Utility.jsonResponse.SUCCESS) {
		                    	$window.location.href = "/PatientFirst/restricted/index.html";
		                    }
		                    else if(responseModel.status == Utility.jsonResponse.INVALID){
		                        Utility.showAlert("alertContainer", "danger", true, 3, "<strong>Invalid Login!</strong> " + responseModel.message);
		                    }
		                }
		            ).error(
		                function(data, status, headers, config) {
		                    Utility.showAlert("alertContainer", "danger", true, 3, "<strong>Error!</strong> Login attempt failed.");
		                }
		            );
				}
			}
			
			$scope.init();
	    }
]);